<?php
namespace app\controllers;
use app\common\corelib\CacheHelper;
use app\common\HttpCurl;
use app\common\PayUtil;
use app\logic\OrderLogic;
use Yii;




class OrderController extends BaseController
{
    const LIMIT = 10;
	public function actionIndex()
	{
            echo "index";
	}

    public function actionNotify(){
	    $request = Yii::$app->request->post();
        file_put_contents(date("Ymd").".txt", json_encode($request)." 日期：".date("Y-m-d H:i:s",time()).PHP_EOL,FILE_APPEND);

        //$request = '{"returnUrl":"http:\/\/wyyx1.082818.com\/#\/order\/detail?order_no=2018081650571001","payResultDesc":"\u652f\u4ed8\u6210\u529f","paygatewayOrderNo":"20180816165559966122","extend1":"http:\/\/wyyx1.082818.com\/#\/order\/detail?order_no","signType":"1","thirdPartyCode":"alipayWapPay","signContent":"N\/SiNYACBmAPh2VuAP\/VVqHFfFZIUJVh3rC3Kuvj9GqXGykcqQg79v2i+GAU1wlBc+9BR++qLOI0jujfK4BHiJ5Bu79+dAuDIpB7H8CLb3NhsWL7fPLPtqGFPtItsYsNGXaBgnoYHNDmRKIXzr4NafgRu5NCv8CTF6Qgz4GHAF8=","charset":"UTF-8","extend2":"{\"productName\":\"openapi\\u4e13\\u7528\\u52ff\\u65391206007\"}","orderTime":"20180816165405","payTime":"20180816165436","password":"","version":"v2.0","currency":"CNY","amount":"0.01","accountName":"wangyiyanxuanshangcheng","orderNo":"2018081650571001","partnerCode":"C000133","payResultCode":"S","notifyUrl":"http:\/\/39.108.226.167\/order\/notify"}';
        //$request = json_decode($request,true);
        $payResult = $request['payResultCode'];
        $order_no = $request['orderNo'];
        $pay_time = $request['payTime'];
        $pay_no = $request['paygatewayOrderNo'];
        $thirdPartyCode = $request['thirdPartyCode'];
        $signContentR = $request['signContent'];
        $orderLogic = new OrderLogic();
        if($payResult=='S'){
            $pu = new PayUtil();
            $signContent = $pu->verify($request);
            if(true || $signContent==$signContentR){
                $orderLogic->paySuccess($order_no,$pay_time,$pay_no,$thirdPartyCode);
                echo '<?xml version=\"1.0\" encoding=\"UTF-8\"?><result>1</result>';exit;
            }else{
                return false;
            }
        }
    }

    public function actionPay(){
        $order_no = $this->getQueryparams('order_no','');
        $orderLogic = new OrderLogic();
        $url = $orderLogic->pay($order_no);
        $this->response(['pay_url'=>$url]);
    }

    public function actionOrderList(){
        $page = $this->getQueryparams('page',1);
        $limit = $this->getQueryparams('limit',10);
        $orderLogic = new OrderLogic();
        $order = $orderLogic->getOrderList($this->user_id,$page,$limit);
        $this->response($order);
    }

    public function actionSubmitOrder(){
        $mall_id = $this->getQueryparams('mall_id',0);
        $goods_id = $this->getQueryparams('goods_id',0);
        $goods_num = $this->getQueryparams('goods_num',1);
        $order_amount = $this->getQueryparams('order_amount',0);
        $consignee = $this->getQueryparams('consignee','');
        $shipping_mobile = $this->getQueryparams('shipping_mobile','');
        $shipping_address = $this->getQueryparams('shipping_address','');
        $id_card = $this->getQueryparams('id_card','');
        $user_remark = $this->getQueryparams('user_remark','');
        $orderLogic = new OrderLogic();
        $result = $orderLogic->submitOrder($this->user_id,$mall_id,$goods_id,$goods_num,$order_amount,$consignee,$shipping_mobile,$shipping_address,$id_card,$user_remark,$order_no,$order_time,$goods_name);
        if($result){
            $pu = new PayUtil();
            $url = $pu->getUrl($order_no,date('YmdHis',strtotime($order_time)),$order_amount,$goods_name);
            $this->response(['payurl'=>$url]);
        }else{
            $this->error(['988888','商品不合法']);
        }

    }

    public function actionOrderDetail(){
        $order_no = $this->getQueryparams('order_no','');
        $orderLogic = new OrderLogic();
        $order = $orderLogic->getOrderDetail($this->user_id,$order_no);
        $this->response($order);
    }

    public function actionRefund(){
        $order_no = $this->getQueryparams('order_no','');
        $reason = $this->getQueryparams('reason','');
        $orderLogic = new OrderLogic();
        $orderLogic->refund($this->user_id,$order_no,$reason);
        $this->response();
    }

}